<template>
  <div class="server-main">
    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">逻辑模型关系</span>
        <div class="button-group">
          <div>
            <el-button
              type="primary"
              size="small"
              @click="handleAddRalationship"
              >新建关系</el-button
            >
          </div>
        </div>
      </div>
      <div class="item-list-warpper">
        <el-table :data="modelRelationList" class="km-table">
          <el-table-column
            type="index"
            label="ID"
            width="50"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="sourceModelCode"
            label="模型编码"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="sourceModelName"
            label="模型名称"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="sourceFiledName"
            label="属性中文名"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="sourceFiledColumn"
            label="属性英文名"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="relationName"
            label="关系名称"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="targetModelCode"
            label="模型编码"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="targetModelName"
            label="模型名称"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="targetFiledName"
            label="属性中文名"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="targetFiledColumn"
            label="属性英文名"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作" align="center" width="250">
            <template slot-scope="scope">
              <el-link
                :underline="false"
                type="primary"
                class="ontology-edit"
                @click.native.prevent="openEditRelation(scope.row)"
                >编辑</el-link
              >
              <el-link
                :underline="false"
                type="primary"
                class="ontology-edit"
                @click.native.prevent="modelDelete(scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="km-pagination"
          background
          :small="true"
          layout="total, prev, pager, next,sizes, jumper"
          :total="total"
          :hide-on-single-page="!total"
          :page-size.sync="modelData.pageSize"
          :pager-count="7"
          :page-sizes="[10, 20, 30]"
          :current-page="modelData.pageNum"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
        <el-dialog
          :title="dialogConfig[dialogType].title"
          v-if="dialogFormVisible"
          :visible.sync="dialogFormVisible"
          @close="afterDialogClose"
          width="60%"
        >
          <add-relation
            :rowData="rowData"
            @saveData="getSaveData"
          ></add-relation>
          <span slot="footer" class="dialog-footer">
            <el-button @click="resetDialogForm">取 消</el-button>
            <el-button type="primary" @click="submitDialog" :loading="loading"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
import AddRelation from "./addRelation.vue";
export default {
  components: { AddRelation },
  data() {
    return {
      // 表格高度
      tableHeight: "52vh",
      // 判断状态为编辑中或已发布
      // 属性列表,
      modelRelationList: [], //模型关系列表
      tableList: [],
      total: 0, // 数据总条数
      typesOptions: [], //数据类型列表
      modelId: -1, //逻辑模型id
      modelData: {
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      modelParams: {
        //编辑模型模板
        logicalModelId: 0,
        modelCode: "", // 模型编码
        modelName: "", // 模型名称
        modelDescription: "", // 模型说明
      },
      dialogType: "ADD",
      dialogConfig: {
        ADD: {
          title: "创建关系",
          funcName: "saveLogicalModelRelation",
        },
        EDIT: {
          title: "编辑关系",
          funcName: "editLogicModelRelation",
        },
      },
      dialogFormVisible: false, //创建、编辑弹窗默认状态
      loading: false,
      saveData: {}, //新增关系弹窗要保存的数据
      rowData: {}, //编辑关系，弹窗要回填的数据
      modelRelationId: null, //编辑选中的模型关系id
    };
  },
  computed: {
    ...mapState({
      selectedData: (state) => state.modelDesign.selectData,
    }),
  },
  created() {
    this.getModelRelations(this.$route.params.id);
  },
  methods: {
    //获取模型关系列表
    getModelRelations(Id) {
      let logicalModelId = { logicalModelId: Id || null };
      let params = { ...this.modelData, ...logicalModelId };
      this.$axios
        .post(this.$api.modelDesign.physicModel.getModelRelationList, params)
        // .post(this.$api.modelDesign.logicModel.getModelRelationList, params)
        .then((res) => {
          console.log(res);
          let data = res.data.data.list;
          this.modelRelationList = data;
          this.total = res.data.data.totalCount;
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err.data.message);
        });
    },
    // 打开编辑模型模态框
    openEditModel(row) {
      this.dialogType = "edit";
      this.modelParams.logicalModelId = row.logicalModelId;
      this.modelParams.modelCode = row.modelCode;
      this.modelParams.modelName = row.modelName;
      this.modelParams.description = row.description;
      this.dialogFormVisible = true;
    },
    // 确定
    submitDialog() {
      console.log("param", JSON.stringify(this.modelParams));
      let func = this.dialogConfig[this.dialogType].funcName;
      this[func]();
      this.dialogFormVisible = false;
    },
    //新增模型关系
    saveLogicalModelRelation() {
      let id = {
        sourceModelId: this.$route.params.id,
      };
      let params = { ...id, ...this.saveData };
      this.$axios
        .post(this.$api.modelDesign.physicModel.addLogicalModelRelation, params)
        .then((res) => {
          console.log(res);
          this.$message({ message: res.data.message, type: "success" });
          let data = res.data.data;
          this.dialogFormVisible = false;
          this.getModelRelations(this.$route.params.id);
        })
        .catch((err) => {
          this.$message.error(err.data.message);
        });
    },
    // 编辑模型关系
    editLogicModelRelation() {
      let {
        sourceModelId,
        sourceColumnId,
        relationName,
        targetModelId,
        targetColumnId,
      } = { ...this.saveData };
      let modelRelationId = { modelRelationId: this.modelRelationId };
      let params = {
        sourceModelId,
        sourceColumnId,
        relationName,
        targetModelId,
        targetColumnId,
        ...modelRelationId,
      };
      console.log(params);
      this.loading = true;
      this.$axios
        .post(
          this.$api.modelDesign.physicModel.editLogicalModelRelation,
          params
        )
        .then((res) => {
          this.loading = false;
          this.$message({ message: "编辑成功!", type: "success" });
          this.$nextTick(() => {
            this.getModelRelations(this.$route.params.id);
          });
        })
        .catch((error) => {
          this.loading = false;
          this.$message.error(error.data.message);
        });
    },
    //删除关系
    modelDelete(row) {
      this.$confirm("是否确认删除该模型关系？ 删除后将无法恢复，请谨慎操作!")
        .then(() => {
          this.loading = true;
          this.$axios
            .post(
              `${this.$api.modelDesign.physicModel.deleteLogicalModelRelation}?modelRelationId=${row.modelRelationId}`
            )
            .then((res) => {
              this.loading = false;
              this.$message({ message: "删除成功!", type: "success" });
              this.getModelRelations(this.$route.params.id);
            })
            .catch((error) => {
              this.loading = false;
              this.$message.error(error.data.message);
            });
        })
        .catch(() => {});
    },
    //获取要保存的数据
    getSaveData(val) {
      console.log("父组件", val);
      this.saveData = val;
    },
    // 取消
    resetDialogForm() {
      this.dialogFormVisible = false;
    },
    // 模态框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getTableList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getTableList();
    },
    //打开新建关系弹窗
    handleAddRalationship() {
      this.rowData = { type: "ADD" };
      this.dialogType = "ADD";
      this.dialogFormVisible = true;
    },
    // 打开编辑关系框(获取节点信息)
    openEditRelation(row) {
      this.dialogType = "EDIT";
      let type = { type: "EDIT" };
      this.rowData = { ...type, ...row };
      this.modelRelationId = row.modelRelationId;
      this.$nextTick(() => {
        this.dialogFormVisible = true;
      });
    },
    // 打开新增本体框
    openCreateOntology() {
      this.dialogType = "ADD";
      this.dialogFormVisible = true;
      this.modelParams.name = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.server-main {
  position: relative;
  .button-group {
    display: flex;
    justify-content: flex-end;
    padding: 16px 0 10px 0;
  }
  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    border-radius: 4px;
    background: #ffffff;
    .item-list-warpper {
      padding-bottom: 24px;
    }
    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
      }

      margin-bottom: 10px;
    }

    .little-circle-green {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #55c898;
      position: relative;
      right: 4px;
      top: -2px;
    }

    .little-circle-red {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #ee5564;
      position: relative;
      right: 6px;
      top: -2px;
    }

    .little-circle-yellow {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #dba839;
      position: relative;
      right: 6px;
      top: -2px;
    }

    .page-wrapper {
      margin-top: 28px;
      display: flex;
      justify-content: flex-end;
      color: rgba(0, 0, 0, 0.65);

      .data-counter {
        font-weight: 400;
        line-height: 32px;
        margin-right: 24px;
      }

      /deep/ .btn-prev,
      /deep/ .btn-next {
        margin: 0 4px;
        min-width: 28px;
        border-radius: 2px;
        border: 1px solid rgba(0, 0, 0, 0.15);
        padding: 0;
      }

      /deep/ .el-pager .number {
        min-width: 28px;
        height: 28px;
        border-radius: 2px;
        border: 1px solid rgba(0, 0, 0, 0.15);
        margin: 0 4px;
        cursor: pointer;
      }
    }
  }
  .ontology-edit {
    padding: 0 10px;

    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }
  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }
}
</style>
